package com.example.bysj.Controller;
//201902104061徐中堂
import com.example.bysj.Service.ActorService;
import com.example.bysj.Service.UserService;
import com.example.bysj.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//声明此类是一个Controller对象，返回指定文件形式
@Controller
//父链接
@RequestMapping("/studentInfoSys")
public class UserController {
    //自动注入
    @Autowired
    private UserService userService;
    private ActorService actorService;
    //根据用户名查找用户
    @GetMapping("/user/{username}")
    @ResponseBody
    public User getFindByUserName(@PathVariable("username")String username){
        User user = userService.getByUserName(username);
        return user;
    }
    //查找全部
    @GetMapping("/user")
    @RequiresPermissions("user:get")
    public List<User> getAllUsers(){
        return userService.findAll();
    }
    //添加用户
    @PostMapping("/user")
    @ResponseBody
    //设置权限，参数为用户名，密码，角色id
    @RequiresPermissions("user:post")
    public String add(@RequestParam("username") String username,
                      @RequestParam("password")String password,@RequestParam("actor_id")int actorId) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        //获取用户对应的actor
        user.setActor(actorService.find(actorId));
        Boolean added=userService.addUser(user);
        if(added) {
            return "添加成功！";
        }else {
            return "添加失败！";
        }
    }

}
